﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GetIntervalCount] 
	-- Add the parameters for the stored procedure here
	@Data DATETIME,
	@Interval NVARCHAR(3)
AS
BEGIN
	
	SET NOCOUNT ON;
	DECLARE @AdsNo as INT;
    
    IF @Interval = 'AM1'
    BEGIN

		SET @AdsNo = (	SELECT COUNT(*)
							FROM AdPlan AP INNER JOIN ClientAd CA 
								ON AP.IDAd = CA.ID
							WHERE 
								YEAR(AP.PlanDate) = YEAR(@Data) AND
								MONTH(AP.PlanDate) = MONTH(@Data) AND
								DAY(AP.PlanDate) = DAY(@Data) AND
								AP.AM1 = 1
						)

    END
    ELSE IF @Interval = 'AM2'
    BEGIN
    
    SET @AdsNo =
		(SELECT COUNT(*)
		FROM AdPlan AP INNER JOIN ClientAd CA 
			ON AP.IDAd = CA.ID
		WHERE 
								YEAR(AP.PlanDate) = YEAR(@Data) AND
								MONTH(AP.PlanDate) = MONTH(@Data) AND
								DAY(AP.PlanDate) = DAY(@Data) AND
			AP.AM2 = 1)
    END

	ELSE IF @Interval = 'AM3'
    BEGIN
    SET @AdsNo =
		(SELECT COUNT(*)
		FROM AdPlan AP INNER JOIN ClientAd CA 
			ON AP.IDAd = CA.ID
		WHERE 
								YEAR(AP.PlanDate) = YEAR(@Data) AND
								MONTH(AP.PlanDate) = MONTH(@Data) AND
								DAY(AP.PlanDate) = DAY(@Data) AND
			AP.AM3 = 1)
    END
    
    ELSE IF @Interval = 'PM1'
    BEGIN
    SET @AdsNo =
		(SELECT COUNT(*)
		FROM AdPlan AP INNER JOIN ClientAd CA 
			ON AP.IDAd = CA.ID
		WHERE 
								YEAR(AP.PlanDate) = YEAR(@Data) AND
								MONTH(AP.PlanDate) = MONTH(@Data) AND
								DAY(AP.PlanDate) = DAY(@Data) AND
			AP.PM1 = 1)
    END
    
    ELSE IF @Interval = 'PM2'
    BEGIN
    SET @AdsNo =
		(SELECT COUNT(*)
		FROM AdPlan AP INNER JOIN ClientAd CA 
			ON AP.IDAd = CA.ID
		WHERE 
								YEAR(AP.PlanDate) = YEAR(@Data) AND
								MONTH(AP.PlanDate) = MONTH(@Data) AND
								DAY(AP.PlanDate) = DAY(@Data) AND
			AP.PM2 = 1)
    END

	SELECT @AdsNo as AdsNo
	--SELECT YEAR(@Data), MONTH(@Data), DAY(@Data)

	END